package org.xyxy.scientificResearch.web;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.Date;
import java.util.Map;
import net.sf.json.JSONObject;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Results;
import org.xyxy.scientificResearch.entity.Dissertation;
import org.xyxy.scientificResearch.service.DissertationService;
import org.apache.struts2.convention.annotation.Result;
import org.hpin.base.usermanager.entity.User;
import org.hpin.common.core.SpringTool;
import org.hpin.common.core.web.BaseAction;
import org.hpin.common.util.HttpTool;
import org.hpin.common.util.StrUtils;
import org.hpin.common.widget.pagination.Page;

@Namespace("/resource")
@Action("dissertationAction")
@Results({
	@Result(name = "addDissertation", location = "/WEB-INF/pages/scientificResearch/addDissertation.jsp"),
	@Result(name = "browDissertationAll", location = "/WEB-INF/pages/scientificResearch/browDissertationAll.jsp"),
	@Result(name = "modifyDissertationAll", location = "/WEB-INF/pages/scientificResearch/modifyDissertationAll.jsp"),
	@Result(name = "modifyDissertation", location = "/WEB-INF/pages/scientificResearch/modifyDissertation.jsp"),
	@Result(name = "browDissertation", location = "/WEB-INF/pages/scientificResearch/browDissertation.jsp"),
	@Result(name = "listDissertation", location = "/WEB-INF/pages/scientificResearch/listDissertation.jsp")
	})
public class DissertationAction extends BaseAction{
	/**
	 * 
	 */
	private static final long serialVersionUID = -8855663657712769464L;
	DissertationService dissertationService=(DissertationService) SpringTool.getBean(DissertationService.class);
	private Dissertation dissertation;
	public String addDissertation(){
		if(StrUtils.isNotNullOrBlank(id)){
			dissertation = (Dissertation) dissertationService.findById(id);
			
			
			HttpTool.setAttribute("dissertation", dissertation);
			
		}else{
			dissertation = new Dissertation();
		}
		return "addDissertation";
		
	}
	/** 
	 * @论文列表
	 */
	public String listDissertation() throws ParseException{
		page = new Page(HttpTool.getPageNum() , HttpTool.getPageSize());
		Map paramsMap = buildSearch();
		String orderField  = HttpTool.getParameter("orderField");
		String orderDirection = HttpTool.getParameter("orderDirection");
		if( paramsMap.containsKey("orderDirection")){
			paramsMap.remove("orderDirection");
		}
		if( paramsMap.containsKey("orderField")){
			paramsMap.remove("orderField");
		}
		if (StrUtils.isNotNullOrBlank(orderField)&&StrUtils.isNotNullOrBlank(orderDirection)) {
		//	paramsMap.put(""+orderField+"",orderDirection);
			paramsMap.put(orderField,orderDirection);
		}
		else {
			paramsMap.put("order_createTime", "desc");
		}
		HttpTool.setAttribute("orderField", orderField);
		HttpTool.setAttribute("orderDirection", orderDirection);
		findByPage(page, paramsMap);
		return "listDissertation";
	}
	public Page findByPage(Page page , Map paramsMap){
		paramsMap.put("filter_and_isDeleted_EQ_I",0);
		paramsMap.put("order_createTime", "desc");
		 dissertationService.findByPage(page, paramsMap);
		return page ;
	}
	/**
	 * @删除论文
	 * @throws IOException
	 */
	public String deleteDissertation() throws IOException{
		JSONObject json = new JSONObject();
		try {
			User currentUser = (User)HttpTool.getSession().getAttribute("currentUser");
			String[] id = ids.replaceAll(" ", "").split(",");
			dissertationService.deleteDissertation(id,currentUser.getAccountName(),new Date());
			json.accumulate("statusCode", 200);
			json.accumulate("message", "操作成功");
			json.accumulate("navTabId", super.navTabId);
			json.accumulate("callbackType", "");
			json.accumulate("forwardUrl", "");
			json.accumulate("confirmMsg", "");
		} catch (Exception e) {
			json.accumulate("statusCode", 300);
			json.accumulate("message", "操作失败");
			e.printStackTrace();
		}
		
		renderJson(json);
		return null;
	}	
	
	/** @添加论文基本信息*/
	public String saveDissertation(){
		JSONObject json = new JSONObject();
		try {
			String currentTabId = HttpTool.getParameter("currentTabId");
			User currentUser = (User)HttpTool.getSession().getAttribute("currentUser");
			if(StrUtils.isNotNullOrBlank(dissertation.getId())){
				if(null!=dissertation){
					System.out.println(dissertation.getId());
					dissertation.setUpdateTime(new Date());
					dissertation.setUpdateUserId(currentUser.getId());
					dissertationService.update(dissertation);
					json.accumulate("statusCode", 200);
					json.accumulate("message", "操作成功");
					json.accumulate("navTabId", super.navTabId);
					json.accumulate("currentTabId", currentTabId);
					json.accumulate("callbackType", "closeCurrent");
				}
			}else{
				System.out.println(dissertation.getAttachment());
				dissertation.setId(null);
				dissertation.setCreateTime(new Date());
				dissertation.setCreateUserId(currentUser.getId());
				dissertation.setIsDeleted(0);
				dissertation.setStatus("1011701");
				dissertationService.save(dissertation);
				json.accumulate("statusCode", 200);
				json.accumulate("message", "操作成功");
				json.accumulate("navTabId", super.navTabId);
				json.accumulate("currentTabId", currentTabId);
				json.accumulate("callbackType", "closeCurrent");
				}
				
				
				
//		json.accumulate("rel", "");
		} catch (Exception e) {
			json.accumulate("statusCode", 300);
			json.accumulate("message", "操作失败");
			e.printStackTrace();
		}
		//json.accumulate("callbackType", dissertation.getId());
		renderJson(json);
		return null;
	}
	public String browDissertationAll() throws ParseException{
		String flag = HttpTool.getParameter("flag");
		//String taskNo = HttpTool.getParameter("taskNo");
		HttpTool.setAttribute("id", id);
		HttpTool.setAttribute("flag", flag);
		//HttpTool.setAttribute("taskNo", taskNo);
		return "browDissertationAll";
	}
	/**
	 * @产看论文详细信息
	 */
	public String browDissertation() throws ParseException{
		String flag = HttpTool.getParameter("flag");
		dissertation = (Dissertation)dissertationService.findById(id);
		HttpTool.setAttribute("flag", flag);
		return "browDissertation";
	}
	/**
	 * @修改
	 */
	public String modifyDissertationAll() throws ParseException{
		HttpTool.setAttribute("id", id);
		return "modifyDissertationAll";
	}
	/**
	 * @论文变更页面
	 */
	public String modifyDissertation() throws ParseException{
		dissertation = (Dissertation)dissertationService.findById(id);
		return "modifyDissertation" ;
	}
	public Dissertation getDissertation() {
		return dissertation;
	}
	public void setDissertation(Dissertation dissertation) {
		this.dissertation = dissertation;
	}
		

}
